package cz.muni.fi.pb138.videoweb2013.servlets;

import cz.muni.fi.pb138.videoweb2013.backend.Medium;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManager;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManagerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;

/**
 * creating new category action
 * 
 * @author Filip
 */
@UrlBinding("/action/NewCategory.jsp")
public class NewCategoryActionBean implements ActionBean{
    private ActionBeanContext context;
    private String catName;
    private String first;
    private String second;
    private String third;
    private String fourth;
    private String fifth;
    private String sixth;

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getThird() {
        return third;
    }

    public void setThird(String third) {
        this.third = third;
    }

    public String getFourth() {
        return fourth;
    }

    public void setFourth(String fourth) {
        this.fourth = fourth;
    }

    public String getFifth() {
        return fifth;
    }

    public void setFifth(String fifth) {
        this.fifth = fifth;
    }

    public String getSixth() {
        return sixth;
    }

    public void setSixth(String sixth) {
        this.sixth = sixth;
    }
    

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    /**
     * creates new category of a medium in case category name and first header attribute are not empty
     * 
     * @return forward resolution to page NewCategoryFinished or to NewCategoryWrong if conditions of non-emptiness are not fulfilled
     */
    @DefaultHandler
    public Resolution addCategory() {
        MediumManager mm = new MediumManagerImpl();
        Map<String,String> mp = new HashMap<String,String>();
        List<String> header = new ArrayList<String>();
        if(catName==null){
            return new ForwardResolution("/NewCategoryWrong.jsp");
        }
        if(first != null){
            header.add(first);
            mp.put(first,"");
        }else{
            return new ForwardResolution("/NewCategoryWrong.jsp");
        }
        if(second != null){
            header.add(second);
            mp.put(second,"");
        }
        if(third != null){
            header.add(third);
            mp.put(third,"");
        }
        if(fourth != null){
           header.add(fourth);
           mp.put(fourth,"");
        }
        if(fifth != null){
            header.add(fifth);
            mp.put(fifth,"");
        }
        if(sixth != null){
            header.add(sixth);
            mp.put(sixth,"");
        }
        Medium med = new Medium(mp, catName, catName.replaceAll("\\s", ""), header);
        int del = mm.addRecord(med);
        Medium medi = new Medium(del, null, null, null, null);
        mm.deleteRecord(medi);
        return new ForwardResolution("/NewCategoryFinished.jsp");
    }
}
